/*
 * @(#)XfslAlgorithmFactory.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model;

import xfuzzy.lang.*;
import xfuzzy.xfsl.model.algorithm.*;

/**
 * Clase construye un algoritmo de aprendizaje
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslAlgorithmFactory implements IXfslAlgorithmConstants {

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Genera un algoritmo utilizando su nombre y par�metros
	 */
	public static XfslAlgorithm createAlgorithm(String name,double[] param)
	throws XflException{
		XfslAlgorithm alg = createAlgorithm(parse(name));
		alg.setParameters(param);
		return alg;
	}

	/**
	 * Genera un algoritmo utilizando su c�digo y par�metros
	 */
	public static XfslAlgorithm createAlgorithm(int code, double[] param)
	throws XflException {
		XfslAlgorithm alg = createAlgorithm(code);
		alg.setParameters(param);
		return alg;
	}

	/**
	 * Genera un algoritmo utilizando su c�digo
	 */
	public static XfslAlgorithm createAlgorithm(int code) throws XflException {
		switch(code) {
		case BACKPROP: return new BackProp();
		case INCREMENTALBACKPROP: return new IncrementalBackPropagation();
		case BACKPROPMOM: return new BackPropMom();
		case ADAPTLEARNRATE: return new AdaptLearnRate();
		case ADAPTSTEPSIZE: return new AdaptStepSize();
		case MANHATTAN: return new Manhattan();
		case QUICKPROP: return new QuickProp();
		case RPROP: return new RProp();
		
		case STEEPEST: return new SteepestDescent();
		case POLAK_RIBIERE: return new PolakRibiere();
		case FLETCHER_REEVES: return new FletcherReeves();
		case HESTENES_STIEFEL: return new HestenesStiefel();
		case ONE_STEP_SECANT: return new OneStepSecant();
		case SCALED: return new ScaledConjugateGradient();
		
		case DFP: return new DavidonFletcherPowell();
		case BFGS: return new BroydenFletcherGoldfarbShanno();
		case MARQUARDT: return new Marquardt();
		case GAUSS: return new GaussNewton();
		
		case BLIND: return new BlindSearch();
		case SIMPLEX: return new Simplex();
		case POWELL: return new Powell();
		
		case LINEAR_ANNEALING: return new LinearAnnealing();
		case EXPONENTIAL_ANNEALING: return new ExponentialAnnealing();
		case CLASSICAL_ANNEALING: return new ClassicalAnnealing();
		case FAST_ANNEALING: return new FastAnnealing();
		case ADAPTIVE_ANNEALING: return new AdaptiveAnnealing();

		default: throw new XflException(28);
		}
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Parser de los identificadores de configuraci�n
	 */
	private static int parse(String name) {
		if(name.equals("IncrementalBackprop")) return INCREMENTALBACKPROP;
		if(name.equals("Backpropagation")) return BACKPROP;
		if(name.equals("Backprop_with_momentum")) return BACKPROPMOM;
		if(name.equals("Adaptive_learning_rate")) return ADAPTLEARNRATE;
		if(name.equals("Adaptive_step_size")) return ADAPTSTEPSIZE;
		if(name.equals("Manhattan")) return MANHATTAN;
		if(name.equals("Quickprop")) return QUICKPROP;
		if(name.equals("Rprop")) return RPROP;

		if(name.equals("Steepest_descent")) return STEEPEST;
		if(name.equals("Polak_Ribiere")) return POLAK_RIBIERE;
		if(name.equals("Fletcher_Reeves")) return FLETCHER_REEVES;
		if(name.equals("Hestenes_Stiefel")) return HESTENES_STIEFEL;
		if(name.equals("One_Step_Secant")) return ONE_STEP_SECANT;
		if(name.equals("Scaled_conjugate_gradient")) return SCALED;

		if(name.equals("Davidon_Fletcher_Powell")) return DFP;
		if(name.equals("Broyden_Fletcher_Goldfarb_Shanno")) return BFGS;
		if(name.equals("Marquardt")) return MARQUARDT;
		if(name.equals("Gauss_Newton")) return GAUSS;
		
		if(name.equals("Blind_search")) return BLIND;
		if(name.equals("Simplex")) return SIMPLEX;
		if(name.equals("Powell")) return POWELL;
		
		if(name.equals("Linear_Simulated_Annealing")) return LINEAR_ANNEALING;
		if(name.equals("Exponential_Simulated_Annealing")) return EXPONENTIAL_ANNEALING;
		if(name.equals("Classical_Simulated_Annealing")) return CLASSICAL_ANNEALING;
		if(name.equals("Fast_Simulated_Annealing")) return FAST_ANNEALING;
		if(name.equals("Adaptive_Simulated_Annealing")) return ADAPTIVE_ANNEALING;

		return -1;
	}

}
